/**
 * 
 */
package co.fitstart.mobile.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.service.impl.GenericServiceImpl;

import co.fitstart.entity.product.Product;
import co.fitstart.entity.product.ProductType;
import co.fitstart.entity.product.Status;
import co.fitstart.entity.product.Tag;
import co.fitstart.entity.product.stat.UserProductBehavior;
import co.fitstart.mobile.dao.support.ProductDaoSupport;
import co.fitstart.mobile.service.ProductService;

/**
 * @author downpour
 *
 */
@Service("productService")
public class ProductServiceImpl extends GenericServiceImpl<Product> implements ProductService {
    
    @Autowired
    private ProductDaoSupport productDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ProductService#list(co.fitstart.entity.product.ProductType)
     */
    @SuppressWarnings("unchecked")
    public List<Product> list(ProductType productType) {

        String sentence = "FROM Product product WHERE product.disabled = false AND product.display = true"
                + " AND product.status = :status AND product.type = :type AND product.buyable = true"
                + " ORDER BY product.weight DESC, createTime DESC";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("type", productType);
        parameters.put("status", Status.ONLINE);
        
        return genericDaoSupport.searchForList(sentence, parameters);

    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ProductService#list(co.fitstart.entity.product.ProductType, co.fitstart.entity.product.Tag)
     */
    public List<Product> list(ProductType productType, Tag tag) {
        
        List<Product> products = this.list(productType);
        
        List<Product> filterProducts = new ArrayList<Product>();
        
        for(Product product : products) {
            if(product.getTags().containsKey(tag.getAlias())) {
                filterProducts.add(product);
            }
        }
        
        if(!filterProducts.isEmpty()) {
            return filterProducts.size() > 2 ? filterProducts.subList(0, 2) : filterProducts;
        }
        
        return null;
        
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ProductService#readProduct(co.fitstart.entity.product.Product, java.lang.String, java.lang.String)
     */
    public void readProduct(Product product, String openid, String source) {
        
        // FIXME add source when udpate user product behavior
        
        UserProductBehavior userProductBehavior = productDaoSupport.loadUserProductBehavior(product, openid);
        
        if(userProductBehavior == null) {
             userProductBehavior = new UserProductBehavior(product, openid);
            genericDaoSupport.save(userProductBehavior);
        }else {
            genericDaoSupport.update(userProductBehavior.read());
        }
        
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ProductService#shareProduct(co.fitstart.entity.product.Product, java.lang.String)
     */
    public void shareProduct(Product product, String openid) {
        
        UserProductBehavior userProductBehavior = productDaoSupport.loadUserProductBehavior(product, openid);
        
        if(userProductBehavior != null && !userProductBehavior.isShared()) {
            genericDaoSupport.update(userProductBehavior.share());
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ProductService#orderProduct(co.fitstart.entity.product.Product, java.lang.String, java.lang.String)
     */
    public void orderProduct(Product product, String openid, String source) {
        
        // FIXME add order source when update user product behavior
        
        UserProductBehavior userProductBehavior = productDaoSupport.loadUserProductBehavior(product, openid);
        
        if(userProductBehavior != null) {
            genericDaoSupport.update(userProductBehavior.order());
        }
        
    }

}
